package com.kaka.bus.engine.other;

import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.base.RxBaseActivity;
import com.kaka.bus.bean.DrainageBean;
import com.kaka.bus.util.DrainageEvent;

import butterknife.BindView;

/**
 * Created by zhangqi on 2017/11/27.
 */

public class DrainageStatActivity extends RxBaseActivity {

    public static final String STAT_URL = "url";
    public static final String STAT_BACKURL = "backurl";
    public static final String STAT_ID = "id";
    public static final String STAT_NAME = "name";
    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.webview_progressbar)
    ProgressBar mWebviewProgressbar;
    @BindView(R.id.webview)
    WebView mWebview;
    private String mBackUrl;
    /**
     * 推广产品的ID
     */
    private String mId;
    /**
     * 推广产品的名字
     */
    private String mName;

    @Override
    protected void initView() {

        initWebView(mWebview, mWebviewProgressbar);
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        String url = intent.getStringExtra(STAT_URL);
        mBackUrl = intent.getStringExtra(STAT_BACKURL);
        mId = intent.getStringExtra(STAT_ID);
        mName = intent.getStringExtra(STAT_NAME);

        mToolbarName.setText("推广注册");
        mToolbarName.setOnClickListener(v -> finish());

        mWebview.loadUrl(url);
    }

    public void initWebView(WebView webView, ProgressBar progressBar) {
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDefaultTextEncodingName("UTF-8");
        settings.setSupportZoom(false);
        settings.setPluginState(WebSettings.PluginState.ON);
        webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    progressBar.setVisibility(View.GONE);
                } else {
                    progressBar.setVisibility(View.VISIBLE);
                    progressBar.setProgress(newProgress);
                }
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (mBackUrl != null) {
                    if (mBackUrl.equals(url)) {
                        stat();
                    }
                }
                return false;
            }
        });
    }

    /**
     * 统计成功
     */
    private void stat() {
        DrainageEvent.event(getUserId(), mId, DrainageBean.EVENT_TYPE_SUCCESS, null);
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_drainage_stat;
    }

}
